<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Models\Setting;
use Illuminate\Http\Request;

class SettingController extends Controller
{
    //展示系统设置表单
    public function index(Setting $setting){
        // 首先获取数据
        $setting = $setting->orderBy('sort','asc')->get();
        // dump($setting);
        $data = [
            'settings'=>$setting,
        ];
        return view('admin.setting.index',$data);
    }

    //保存配置 save方法需要获取请求，这里我们使用依赖注入
    public function save(Request $request,Setting $setting){
        $settings = $request->input('settings');
        // dump($settings);
        foreach($settings as $key=>$value){
            // 判断value是否完全为null
            $value = ($value===null)?"":$value;
            // 这里需要写入所以需要数据模型
            $setting->where('key','=',$key)->update(['value'=>$value]);
        }
        alert('操作成功');
        return redirect()->route('admin.setting');
    }
}
